//
//  LTM_DataDeal.m
//  LTM_Lib_Network_Objc
//
//  Created by 柯南 on 2021/12/7.
//

#import "LTM_DataDeal.h"

#import <YYModel/YYModel.h>

@implementation LTM_DataDeal
+ (void)requestDealSuccess:(id)response
                    Target:(Class)className
                   Success:(void(^)(NSObject *model))success
                   Failure:(void(^)(NSError *error))failure{    
    if (response != nil) {
        NSInteger code = [response[@"code"] integerValue];
        switch (code) {
            case 200:
                [self successDeal200:response Target:className Success:success Failure:failure];
                break;
                
            case 400:
                [self successDeal400:response Failure:failure];
                break;

            case 403:
                [self successDeal403:response Failure:failure];
                break;
                
            case 500:
                [self successDeal500:response Failure:failure];
                break;
                
            default:
                [self successDealOther:response Failure:failure];
                break;
        }
    }else{
        if(failure){
            NSError *error = [NSError errorWithDomain:@"数据请求无返回数据" code:200 userInfo:nil];
            failure(error);
        }
    }
}

+ (void)requestDealFailure:(NSError *)error
                   Failure:(void(^)(NSError *error))failure{
    if(failure){
        failure(error);
    }
}

+ (void)successDeal200:(NSDictionary *)response
                Target:(Class)className
               Success:(void(^)(NSObject *model))success
               Failure:(void(^)(NSError *error))failure{
    NSObject *obj = [[NSObject alloc] init];
    if ([response[@"entity"] isKindOfClass:[NSDictionary class]]) {
        obj = [className yy_modelWithJSON:response[@"entity"]];
    }else{
        obj = [className yy_modelWithJSON:response];
    }
    if (success && obj != nil) {
        success(obj);
    }else if(failure){
        NSError *error = [NSError errorWithDomain:@"数据转模型处理失败" code:200 userInfo:response[@"message"]];
        failure(error);
    }
}

+ (void)successDeal400:(NSDictionary *)response
               Failure:(void(^)(NSError *error))failure{
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationNetwork400 object:nil userInfo:response];
}

+ (void)successDeal403:(NSDictionary *)response
               Failure:(void(^)(NSError *error))failure{
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationNetwork403 object:nil userInfo:response];
}

+ (void)successDeal500:(NSDictionary *)response
               Failure:(void(^)(NSError *error))failure{
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationNetwork500 object:nil userInfo:response];
}

+ (void)successDealOther:(NSDictionary *)response
               Failure:(void(^)(NSError *error))failure{
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationNetworkOthoer object:nil userInfo:response];
}

@end
